// pages/addbaoxiu/addbaoxiu.js

const { chaxunjineng,addjineng } =require('../../util/api')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imglist:[],
    imgUrls:"",
    remarks:'',
    heqId:"",
    workImgs:"",
    list:[],
    isSelect:false,//展示类型？
    types:['免费','付费'],
    type:"",//类型
 
    
 

  },

   //点击控制下拉框的展示、隐藏
   select:function(){
    var isSelect = this.data.isSelect
    this.setData({ isSelect:!isSelect})
  },
  //点击下拉框选项，选中并隐藏下拉框
  getType:function(e){
   
    let value = e.currentTarget.dataset.type
    this.setData({
      type:value ,
      isSelect: false,
      classifyId:e.currentTarget.dataset.data.id,
      
      
     
    })
  },
  input(e){
   
     this.setData({
      title:e.detail.value
     })
    

  },
  add(){
    if(this.data.imglist.length >= 5)
    {
      wx.showToast({
        title: '最大上传5张照片',
        icon:'none'
      })
      return
    }
    let that=this
    let imgs = this.data.imglist
    wx.chooseMedia({
      count:1,
      mediaType:['image'],
      sourceType:['album', 'camera'],
      success(res) {
        imgs.push(res.tempFiles[0])
        that.setData({
          imglist:imgs
        })
        wx.uploadFile({
          filePath: res.tempFiles[0].tempFilePath,
          name: 'file',
          url: 'https://anjingbao.anjingbao.net/prod-api/api/common/appUpload',
          success (res){
            let jsonS = JSON.parse(res.data)
            that.setData({
              imgUrls:that.data.imgUrls + jsonS.fileName + ","
            })
            
           
            //do something
          }
        })
      }

    })

  },


  put(){
    this.setData({
      memberId:wx.getStorageSync('memberId'),
      memberName:wx.getStorageSync('memberName')
    })
     let p ={
      memberId:this.data.memberId,
      memberName:wx.getStorageSync('memberName'),
      
      title:this.data.title,
      img:this.data.imgUrls,
      classifyId:this.data.classifyId
 
     }
        
     addjineng(p).then(res=>{
       
       if(res.code==200){
         wx.showModal({
           title: "添加成功",
           icon: 'success',
           duration: 2000
         })
         wx.navigateBack({
           delta: 1
         })
         }
        else{
          wx.showToast({
            title: res.msg,
            icon: 'error',
            duration: 3000
          })
    
        }
     
     
   })
  
 },
 
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    
    
    this.data.heqId=options.heqId
    
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    chaxunjineng().then(res=>{
      this.setData({
        list:res.data
      })
    
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '分享小程序',
      imageUrl: 'https://anjingbao.anjingbao.net/profile/upload/renwufenxiang.jpg',
      path:"/pages/shouye/shouye",//当前页面 path ，必须是以 / 开头的完整路径
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
  }
})